package com.yykj.user.center.server.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author chenl
 * @version 1.0
 * @date 2021/10/31 20:16
 */
public class StringUtil {
    public static List<String> nameSplit(String name) {
        String[] bai = { "欧阳", "太史", "上官", "端木", "司马", "东方", "独孤", "南宫", "万俟", "闻人", "夏侯", "诸葛", "尉迟", "公羊", "赫连", "澹台",
                "皇甫", "宗政", "濮阳", "公冶", "太叔", "申屠", "公孙", "慕容", "仲孙", "钟离", "长孙", "宇文", "司徒", "鲜于", "司空", "闾丘", "子车",
                "亓官", "司寇", "巫马", "公西", "颛孙", "壤驷", "公良", "漆雕", "乐正", "宰父", "谷梁", "拓跋", "夹谷", "轩辕", "令狐", "段干", "百里",
                "呼延", "东郭", "南门", "羊舌", "微生", "公户", "公玉", "公仪", "梁丘", "公仲", "公上", "公门", "公山", "公坚", "左丘", "公伯", "西门",
                "公祖", "第五", "公乘", "贯丘", "公皙", "南荣", "东里", "东宫", "仲长", "子书", "子桑", "即墨", "达奚", "褚师", "吴铭" };
        String newName = name.substring(0, 2);
        List<String> names = new ArrayList<String>();
        boolean flag = false;
        for (int i = 0; i < bai.length; i++) {
            if (newName.equals(bai[i])) {
                names.add(newName);
                names.add(name.substring(2));
                flag = true;
                break;
            }
        }
        if (!flag) {
            names.add(name.charAt(0) + "");
            names.add(name.substring(1));
            return names;
        }
        return names;
    }

    public static int string2int(String str) {
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
        }
        return 0;
    }

    // strDate="19791108" 返回年龄,异常就返回 0
    public static int getAge(String strDate) {

        if (StringUtils.isEmpty(strDate)) {
            return 0;
        }
        strDate = strDate.replaceAll("-", "");
        if("00000000".equals(strDate)){
            return 0;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date birthDay = null;
        try {
            birthDay = sdf.parse(strDate);
        } catch (ParseException e) {
            return 0;
        }
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth)
                    age--;
            } else {
                age--;
            }
        }
        return age;
    }
    public static String LocalDateTime2String(LocalDateTime localDateTime){
        return LocalDateTime2String(localDateTime, "yyyy-MM-dd HH:mm:ss");
    }
    public static String LocalDateTime2String(LocalDateTime localDateTime, String format){
        if(localDateTime != null){
            try {
                DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
                return localDateTime.format(df);
            }catch (Exception e){}
        }
        return null;
    }
}
